package day05;

import java.util.Arrays;

public class ArrayDemo {
    public static void main(String[] args) {
        //数组定义
        /*
        int[] a = new int[5];
        double[] d = new double[10];
        boolean[] b = new boolean[26];//默认值为false

         */
        //初始化
        /*
        int[] arr1 = new int[3];
        int[] arr2 = {2,5,8};
        int[] arr3 = new int[]{2,5,8};
        int[] arr4;
        arr4 = new int[]{2,5,8};

         */
        //数组的访问
        /*
        int[] arr = new int[3];
        System.out.println("数组的长度:"+arr.length);//3 长度即元素个数
        System.out.println("数组的第一个元素为:"+arr[0]);//0
        arr[0] = 100;
        arr[1] = 200;
        arr[2] = 300;
        System.out.println(arr[2]);//300输出最后一个元素的值
        System.out.println(arr[arr.length-1]);//300最后一个元素的值

         */
        //数组的遍历
        /*
        int[] arr = new int[10];
        for (int i=0;i<arr.length;i++){
            arr[i] = (int)(Math.random()*100);
            System.out.println(arr[i]);
        }

         */
        //数组的复制
        //
        /*
        int[] a = {10,20,30,40,50};
        int[] b = new int[6];
        System.arraycopy(a,2,b,4,2);//源数组,源数组起始位置,目标数组,目标数组起始位置,复制长度
        for (int i=0;i<b.length;i++){
            System.out.println(b[i]);
        }

         */
        //整个复制
        /*
        int[] a = {10,20,30,40,50};
        int[] b = Arrays.copyOf(a,6);//newlength长了用默认值补末尾,短了末尾截掉
        for (int i=0;i< b.length;i++){
            System.out.println(b[i]);
        }

         */
        //数组的扩容,创建一个更大的数组把数据复制进去
        int[] a = {10,20,30,40,50};
        a = Arrays.copyOf(a,a.length+2);
        for (int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
}
